import { useSettingsStore } from '@/stores/settingsStore';
import type { ParseResult } from './types';
import * as domParser from './domParserWrapper';
import * as fastXmlParser from './fastXmlParserWrapper';
import * as libxml2WasmParser from './libxml2WasmWrapper';

/**
 * Selects and uses the appropriate XML parser based on user settings.
 * 
 * @param content The XML string content.
 * @returns A Promise resolving to the ParseResult, as libxml2 might be async.
 */
export async function parseWithSelectedParser(content: string): Promise<ParseResult> {
    const settingsStore = useSettingsStore();
    const selected = settingsStore.selectedParser;

    console.log(`Using selected parser: ${selected}`);

    try {
        switch (selected) {
            case 'fast-xml':
                return fastXmlParser.parse(content);
            case 'libxml2-wasm':
                // Ensure libxml2 WASM is loaded/initialized before calling parse
                // This might require an async initialization step elsewhere
                // For now, assume it's ready
                return libxml2WasmParser.parse(content);
            case 'dom':
            default:
                return domParser.parse(content);
        }
    } catch (error: any) {
        // Catch any unexpected errors during parser selection/execution
        console.error(`[ParserSelector] Failed to parse using ${selected}:`, error);
        return {
            success: false,
            error: `Parser Execution Error: ${error.message || 'Unknown error'}`,
            nodes: [],
            edges: []
        };
    }
} 